//
//  IGShareModel.m
//  jingdongfang
//
//  Created by BOE on 2017/9/21.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGShareModel.h"

@implementation IGShareModel

/*
 http ---> https
 nil ---> [UIImage imageNamed:@"AppIcon"]
 */
- (NSString *)convertThumImage {
    NSString *thumbURLStr = self.thumbURL;
    id thumImage = thumbURLStr;
    if (thumbURLStr && ![thumbURLStr isEqualToString:@""]) {
        if (![thumbURLStr containsString:@"https"] && [thumbURLStr containsString:@"http"] && thumbURLStr.length > 4) {
            thumImage = [NSString stringWithFormat:@"https%@", [thumbURLStr substringFromIndex:4]];
        }
    } else {
        thumImage = [UIImage imageNamed:@"AppIcon"];
    }
    return thumImage;
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //组装分享的小图
    id thumImage = [self convertThumImage];
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.title descr:self.descr thumImage:thumImage];
    //设置网页地址
    shareObject.webpageUrl = self.webpageUrl;
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;

    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
        if (error) {
            if (error.code != 2009) {
//                [self showErrorText:@"分享失败"];
                [self showErrorText:NSLocalizedString(@"Sharing Failed", nil)];
            }
        } else {
//            [self showSuccessText:@"分享成功"];
            [self showSuccessText:NSLocalizedString(@"Sharing Successful", nil)];
        }
    }];
}

@end
